home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3c / getaddrinfo.z / getaddrinfo
Encoding:
Text File  |  2002-10-03  |  20.7 KB  |  397 lines

  1.  
  2.  
  3.  
  4. GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))                                                  GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo,,,, ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo,,,, ggggaaaaiiii____ssssttttrrrreeeerrrrrrrroooorrrr - nodename-to-address translation
  10.      in a protocol-independent manner
  11.  
  12. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  13.      ####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ttttyyyyppppeeeessss....hhhh>>>>
  14.      ####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ssssoooocccckkkkeeeetttt....hhhh>>>>
  15.      ####iiiinnnncccclllluuuuddddeeee <<<<nnnneeeettttddddbbbb....hhhh>>>>
  16.  
  17.      iiiinnnntttt ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo((((ccccoooonnnnsssstttt cccchhhhaaaarrrr ****nnnnooooddddeeeennnnaaaammmmeeee,,,, ccccoooonnnnsssstttt cccchhhhaaaarrrr ****sssseeeerrrrvvvvnnnnaaaammmmeeee,,,,
  18.                ccccoooonnnnsssstttt ssssttttrrrruuuucccctttt aaaaddddddddrrrriiiinnnnffffoooo ****hhhhiiiinnnnttttssss,,,, ssssttttrrrruuuucccctttt aaaaddddddddrrrriiiinnnnffffoooo ********rrrreeeessss))));;;;
  19.      vvvvooooiiiidddd ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo((((ssssttttrrrruuuucccctttt aaaaddddddddrrrriiiinnnnffffoooo ****aaaaiiii))));;;;
  20.      ccccoooonnnnsssstttt cccchhhhaaaarrrr ****ggggaaaaiiii____ssssttttrrrreeeerrrrrrrroooorrrr((((iiiinnnntttt eeeeccccooooddddeeee))));;;;
  21.  
  22. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  23.      GGGGeeeettttaaaaddddddddrrrriiiinnnnffffoooo function is defined for protocol-independent nodename-to-
  24.      address translation.  It performs the functionality of ggggeeeetttthhhhoooossssttttbbbbyyyynnnnaaaammmmeeee((((3333NNNN))))
  25.      and ggggeeeettttsssseeeerrrrvvvvbbbbyyyynnnnaaaammmmeeee((((3333NNNN)))),,,, but in a more sophisticated manner.
  26.  
  27.      The addrinfo structure is defined as a result of including the <_n_e_t_d_b._h>
  28.      header:
  29.  
  30.      struct addrinfo {
  31.  
  32.           int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME,
  33.                                   AI_NUMERICHOST , AI_ALL etc */
  34.           int     ai_family;    /* AF_xxx */
  35.           int     ai_socktype;  /* SOCK_xxx */
  36.           int     ai_protocol;  /*0 or IPPROTO_xxx for IPv4 and IPv6*/
  37.           socklen_t  ai_addrlen;   /* length of ai_addr */
  38.           char   *ai_canonname; /* canonical name for nodename */
  39.           struct sockaddr  *ai_addr; /* binary address */
  40.           struct addrinfo  *ai_next; /*next structure in linked list*/
  41.      };
  42.  
  43.      The _n_o_d_e_n_a_m_e and _s_e_r_v_n_a_m_e arguments are pointers to null-terminated
  44.      strings or NULL.  One or both of these two arguments must be a non-NULL
  45.      pointer. In the normal client scenario, both the _n_o_d_e_n_a_m_e and _s_e_r_v_n_a_m_e
  46.      are specified.  In the normal server scenario, only the _s_e_r_v_n_a_m_e is
  47.      specified. A non-NULL _n_o_d_e_n_a_m_e string can be either a node name or a
  48.      numeric host address string i.e., a dotted-decimal IPv4 address or an
  49.      IPv6 hex address. A non-NULL _s_e_r_v_n_a_m_e string can be either a service name
  50.      or a decimal port number.
  51.  
  52.      The caller can optionally pass an addrinfo structure, pointed to by the
  53.      third argument, to provide hints concerning the type of socket that the
  54.      caller supports.  In this _h_i_n_t_s structure all members other than
  55.      ai_flags, ai_family, ai_socktype, and ai_protocol must be zero or a NULL
  56.      pointer. A value of AF_UNSPEC for ai_family means the caller will accept
  57.      any protocol family.  A value of 0 for ai_socktype means the caller will
  58.      accept any protocol.  For example, if the caller handles only TCP and not
  59.      UDP, then the ai_socktype member of the hints structure should be set to
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))                                                  GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))
  71.  
  72.  
  73.  
  74.      SOCK_STREAM  and the ai_protocol member should be set to IPPROTO_TCP when
  75.      ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is called. If the caller handles only IPv4 and not IPv6,
  76.      then the ai_family member of the _h_i_n_t_s structure should be set to AF_INET
  77.      when ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is called.  If the third argument to ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is
  78.      a NULL pointer, this is the same as if the caller had filled in an
  79.      addrinfo structure initialized to zero with ai_family set to AF_UNSPEC.
  80.      The ai_flags field to which _h_i_n_t_s parameter points must have the value
  81.      zero or be the bitwise OR of one or more of the values AI_PASSIVE,
  82.      AI_CANONNAME, AI_NUMERICHOST, AI_NUMERICSERV, AI_V4MAPPED, AI_ALL, and
  83.      AI_ADDRCONFIG.
  84.  
  85.      If the AI_PASSIVE bit is set in the ai_flags member of the _h_i_n_t_s
  86.      structure, then the caller plans to use the returned socket address
  87.      structure in a call to bind().  In this case, if the _n_o_d_e_n_a_m_e argument is
  88.      a NULL pointer, then the IP address portion of the socket address
  89.      structure will be set to INADDR_ANY for an IPv4 address or
  90.      IN6ADDR_ANY_INIT for an IPv6 address.
  91.  
  92.      If the AI_PASSIVE bit is not set in the ai_flags member of the hints
  93.      structure, then the returned socket address structure will be ready for a
  94.      call to ccccoooonnnnnnnneeeecccctttt(((()))) (for a connection-oriented protocol) or either
  95.      ccccoooonnnnnnnneeeecccctttt(((()))),,,, sssseeeennnnddddttttoooo(((()))),,,, oooorrrr sssseeeennnnddddmmmmssssgggg(((()))) (for a connectionless protocol).  In
  96.      this case, if the _n_o_d_e_n_a_m_e argument is a NULL pointer, then the IP
  97.      address portion of the socket address structure will be set to the
  98.      loopback address.
  99.  
  100.      If the AI_CANONNAME bit is set in the ai_flags member of the _h_i_n_t_s
  101.      structure, then upon successful return the ai_canonname member of the
  102.      first addrinfo structure in the linked list will point to a
  103.      null-terminated string containing the canonical name of the specified
  104.      _n_o_d_e_n_a_m_e. If the canonical name is not available, then ai_canonname will
  105.      refer to the nodename argument or a string with the same contents.
  106.  
  107.      If the AI_NUMERICHOST bit is set in the ai_flags member of the _h_i_n_t_s
  108.      structure, then a non-NULL _n_o_d_e_n_a_m_e string must be a numeric host address
  109.      string.  Otherwise an error of EAI_NONAME is returned.  This flag
  110.      prevents any type of name resolution service (e.g., the DNS) from being
  111.      called.
  112.  
  113.      If the flag AI_NUMERICSERV is specified then a non-null servname string
  114.      must be a numeric port string.  Otherwise an error EAI_NONAME is
  115.      returned.  This flag prevents any type of name resolution service (for
  116.      example, NIS+) from being invoked.
  117.  
  118.      If the AI_V4MAPPED flag is specified along with an ai_family of AF_INET6,
  119.      then the caller will accept IPv4-mapped IPv6 addresses.  That is, if no
  120.      AAAA or A6 records are found then a query is made for A records and any
  121.      found are returned as IPv4-mapped IPv6 addresses (ai_addrlen will be 16).
  122.      The AI_V4MAPPED flag is ignored unless ai_family equals AF_INET6.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))                                                  GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))
  137.  
  138.  
  139.  
  140.      The AI_ALL flag should be used in conjunction with the AI_V4MAPPED flag,
  141.      and is only used with an ai_family of AF_INET6.  When AI_ALL is logically
  142.      or'd with AI_V4MAPPED flag then the caller will accept all addresses:
  143.      IPv6 and IPv4-mapped IPv6.  A query is first made for AAAA/A6 records and
  144.      if successful, the IPv6 addresses are returned.  Another query is then
  145.      made for A records and any found are returned as IPv4-mapped IPv6
  146.      addresses (ai_addrlen will be 16).  This flag is ignored unless ai_family
  147.      equals AF_INET6.  When ai_family is not specified (AF_UNSPEC),
  148.      AI_V4MAPPED and AI_ALL flags will only be used if AF_INET6 is supported.
  149.  
  150.      If the AI_ADDRCONFIG flag is specified then a query for AAAA or A6
  151.      records occurs only if the node has at least one IPv6 source address
  152.      configured and a query for A records occurs only if the node has at least
  153.      one IPv4 source address configured.  The loopback address is not
  154.      considered for this case as valid as a configured sources address.
  155.  
  156.      The ai_socktype field to which argument hints points specifies the socket
  157.      type for the service.  If a specific socket type is not given (for
  158.      example, a value of zero) and the service name could be interpreted as
  159.      valid with multiple supported socket types, this implementation will
  160.      attempt to resolve the service name for all supported socket types and,
  161.      all successful results will be returned.  A non-zero socket type value
  162.      will limit the returned information to values with the specified socket
  163.      type.
  164.  
  165.      Upon successful return of ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, the location to which _r_e_s points
  166.      refers to a linked list of addrinfo structures, each of which specifies a
  167.      socket address and information for use in creating a socket with which to
  168.      use that socket address.  The caller can process each addrinfo structure
  169.      in this list by following the ai_next pointer, until a NULL pointer is
  170.      encountered.  In each returned addrinfo structure the three members
  171.      ai_family, ai_socktype, and ai_protocol are the corresponding arguments
  172.      for a call to the ssssoooocccckkkkeeeetttt(((()))) function.  In each addrinfo structure the
  173.      ai_addr member points to a filled-in socket address structure whose
  174.      length is specified by the ai_addrlen member.
  175.  
  176.  
  177.      All of the information returned by ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is dynamically
  178.      allocated: the addrinfo structures, the socket address structures, and
  179.      canonical node name strings pointed to by the addrinfo structures.  To
  180.      return this information to the system the function ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo(((()))) is
  181.      called.  The ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo(((()))) function frees one or more addrinfo
  182.      structures returned by ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, along with any additional storage
  183.      associated with those structures.  If the ai_next field of the structure
  184.      is not null, the entire list of structures is freed.  The ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo(((())))
  185.      function supports the freeing of arbitrary sublists of an addrinfo list
  186.      originally returned by ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((())))....
  187.  
  188.      Functions ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) and ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo(((()))) are thread-safe.
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))                                                  GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))
  203.  
  204.  
  205.  
  206.      To aid applications in printing error messages based on the EAI_xxx codes
  207.      returned by ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, ggggaaaaiiii____ssssttttrrrreeeerrrrrrrroooorrrr(((()))) is defined.  The argument is
  208.      one of the EAI_xxx values defined earlier and the return value points to
  209.      a string describing the error.  If the argument is not one of the EAI_xxx
  210.      values, the function still returns a pointer to a string whose contents
  211.      indicate an unknown error.
  212.  
  213. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  214.      The following code tries to connect to "www.company.example" service
  215.      "ftp" via stream socket.  It loops through all the addresses available,
  216.      regardless of the address family.  If the destination resolves to IPv4
  217.      address, it will use AF_INET socket.  Similarly, if it resolves to IPv6,
  218.      AF_INET6 socket is used.  Observe that there is not hardcoded reference
  219.      to particular address family.
  220.  
  221.  
  222.           #include <stdio.h>
  223.           #include <sys/socket.h>
  224.           #include <netdb.h>
  225.  
  226.           main(){
  227.             struct addrinfo hints, *res, *res0;
  228.             int error;
  229.             int s;
  230.             const char *cause = NULL;
  231.  
  232.  
  233.             memset(&hints, 0, sizeof(hints));
  234.             hints.ai_family = AF_UNSPEC;
  235.             hints.ai_socktype = SOCK_STREAM;
  236.             error = getaddrinfo("www.company.example", "ftp",
  237.                          &hints, &res0);
  238.             if(error){
  239.                fprintf(stderr, "%s", gai_strerror(error));
  240.                   exit(1);
  241.             }
  242.             s = -1;
  243.             for (res = res0; res; res = res->ai_next) {
  244.                s = socket(res->ai_family, res->ai_socktype,
  245.                          res->ai_protocol);
  246.                if (s < 0) {
  247.                     cause = "Error: socket";
  248.                     continue;
  249.                }
  250.                if(connect(s, res->ai_addr, res->ai_addrlen) < 0 ) {
  251.                     cause = "Error: connect";
  252.                     close(s);
  253.                     s = -1;
  254.                     continue;
  255.                }
  256.                cause = "Success";
  257.                break; /* okay we got one */
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))                                                  GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))
  269.  
  270.  
  271.  
  272.             }
  273.           fprintf(stderr, cause);
  274.           freeaddrinfo(res0);
  275.           } /* end of main */
  276.  
  277.      The following example tries to open wildcard listening socket onto
  278.      service "ftp", for all the address families available.
  279.  
  280.  
  281.           #include <stdio.h>
  282.           #include <sys/socket.h>
  283.           #include <netdb.h>
  284.           #define MXSOCK 5
  285.  
  286.           main(){
  287.  
  288.             struct addrinfo hints, *res, *res0;
  289.             int error;
  290.             int s[MXSOCK];
  291.             int n;
  292.             const char *cause = NULL;
  293.  
  294.  
  295.             memset(&hints, 0, sizeof(hints));
  296.             hints.ai_family = AF_UNSPEC;
  297.             hints.ai_socktype = SOCK_STREAM;
  298.             hints.ai_flags = AI_PASSIVE;
  299.             error = getaddrinfo(NULL, "ftp", &hints, &res0);
  300.             if(error) {
  301.                fprintf(stderr, "%s", gai_strerror(error));
  302.                exit(1);
  303.             }
  304.             n = 0;
  305.             for (res = res0; res && n < MXSOCK; res = res->ai_next){
  306.                s[n] = socket(res->ai_family, res->ai_socktype,
  307.                            res->ai_protocol);
  308.                if(s[n] < 0) {
  309.                     cause = "Error: socket";
  310.                     continue;
  311.                }
  312.                if (connect (s[n], res->ai_addr, res->ai_addrlen) < 0){
  313.                     cause = "Error: connect";
  314.                     close(s[n]);
  315.                     continue;
  316.                }
  317.                cause = "Success";
  318.                n++;
  319.             }
  320.             fprintf(stderr, cause);
  321.             freeaddrinfo(res0);
  322.           }
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))                                                  GGGGEEEETTTTAAAADDDDDDDDRRRRIIIINNNNFFFFOOOO((((3333))))
  335.  
  336.  
  337.  
  338. FFFFIIIILLLLEEEESSSS
  339.      ////eeeettttcccc////hhhhoooossssttttssss
  340.      ////eeeettttcccc////iiiippppnnnnooooddddeeeessss
  341.      ////eeeettttcccc////rrrreeeessssoooollllvvvv....ccccoooonnnnffff
  342.  
  343. DDDDIIIIAAAAGGGGNNNNOOOOSSSSTTTTIIIICCCCSSSS
  344.      Error return status from ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is zero on success and non-zero on
  345.      errors.  Non-zero error codes are defined in <netdb.h>, and as follows:
  346.  
  347.      EAI_ADDRFAMILY      Address family for nodename not supported.
  348.      EAI_AGAIN           Temporary failure in name resolution.
  349.      EAI_BADFLAGS        Invalid value for ai_flags.
  350.      EAI_FAIL            Non-recoverable failure in name resolution.
  351.      EAI_FAMILY          ai_family not supported.
  352.      EAI_MEMORY          Memory allocation failure.
  353.      EAI_NODATA          No address associated with nodename.
  354.      EAI_NONAME          nodename nor servname provided, or not known.
  355.      EAI_SERVICE         servname not supported for ai_socktype.
  356.      EAI_SOCKTYPE        ai_socktype not supported.
  357.      EAI_SYSTEM          System error returned in errno.
  358.  
  359.  
  360.      If called with proper argument, ggggaaaaiiii____ssssttttrrrreeeerrrrrrrroooorrrr(((()))) returns a pointer to a
  361.      string describing the given error code.  If the argument is not one of
  362.      the EAI_xxx values, the function still returns a pointer to a string
  363.      whose contents indicate an unknown error.
  364.  
  365. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  366.      ggggeeeettttnnnnaaaammmmeeeeiiiinnnnffffoooo((((3333)))),,,, ggggeeeetttthhhhoooossssttttbbbbyyyynnnnaaaammmmeeee((((3333)))),,,, ggggeeeettttiiiippppnnnnooooddddeeeebbbbyyyynnnnaaaammmmeeee((((3333)))),,,, ggggeeeettttsssseeeerrrrvvvvbbbbyyyynnnnaaaammmmeeee((((3333)))),,,,
  367.      hhhhoooossssttttssss((((5555)))),,,, sssseeeerrrrvvvviiiicccceeeessss((((5555)))),,,, ssssoooocccckkkkeeeetttt((((3333)))),,,, ccccoooonnnnnnnneeeecccctttt((((3333))))....
  368.  
  369. SSSSTTTTAAAANNNNDDDDAAAARRRRDDDDSSSS
  370.      The ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, and ggggaaaaiiii____ssssttttrrrreeeerrrrrrrroooorrrr(((()))) functions are
  371.      defined in IEEE POSIX 1003.1g draft specification, draft-ietf-ipngwg-
  372.      rfc2553bis-03.txt, and documented in "Basic Socket Interface Extensions
  373.      for IPv6" (RFC2553).
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.